<template>
  <el-dialog
    center
    :visible="dialogVisible "
    width="1200px"
    title="选择项目"
    append-to-body
    :before-close="handleClose"
    @closed="handleClose"
    :close-on-click-modal="false"
  >
    <el-form
      :model="queryParams"
      ref="queryParams"
      size="small"
      :inline="true"
    >
      <el-form-item label="项目编号">
        <el-input
          v-model="queryParams.projectCode"
          clearable
          style="width: 200px"
          placeholder="请输入项目编号"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="客户类型">
        <el-select
          v-model="queryParams.customerType"
          placeholder="请选择客户类型"
          style="width: 200px"
        >
          <el-option
            v-for="dict in customerList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称">
        <el-input
          v-model="queryParams.customerNickName"
          clearable
          style="width: 200px"
          placeholder="请输入客户名称"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="代理商">
        <el-input
          v-model="queryParams.submitAgentNickName"
          clearable
          style="width: 200px"
          placeholder="请输入代理商"
        >
        </el-input>
      </el-form-item>
      <!-- <el-form-item label="立项时间">
        <el-date-picker
          v-model="queryParams.beginTime"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          style="width: 200px"
          placeholder="请选择立项时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="立项时间">
        <el-date-picker
          v-model="queryParams.data"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :default-time="['00:00:00', '23:59:59']"
          :style="{width: '100%'}"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      style="width: 100%;"
      height="500"
    >
      <el-table-column
        label="项目编号"
        align="center"
        prop="projectCode"
        width="200px"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        label="客户名称"
        align="center"
        prop="customerNickName"
        width="200px"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="代理商"
        align="center"
        prop="submitAgentNickName"
        width="200px"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        label="合同金额"
        align="center"
        prop="contractAmount"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ formatReceiveAmount(scope.row.contractAmount) }}
        </template>
      </el-table-column>
      <el-table-column
        label="立项时间"
        align="center"
        prop="createTime"
        width="150px"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleOption(scope.row)"
          >选择
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script>
import { projectApplyList, checkContractCreate, projectApplyInfo } from '@/api/processManage/approveForms'
import { formatMoney } from '@/utils/utils';
export default {
  dicts: ['sys_user_sex'],
  props: ['checkShow'],
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectCode: '',
        customerType: '',
        customerNickName: '',
        submitAgentNickName: '',
        beginTime: '',
        endTime: '',
        data: []
      },
      total: 0,
      loading: false,
      customerList: [
        {
          label: '全部',
          value: ''
        },
        {
          label: '法人',
          value: '01'
        },
        {
          label: '自然人',
          value: '02'
        },
      ],
    };
  },
  methods: {
    formatReceiveAmount(amount) {
      return formatMoney(amount);
    },
    async sync() {
      this.dialogVisible = true;
      this.resetQuery()
    },
    // 列表
    getList() {
      this.tableData = []
      this.loading = true
      let params = JSON.parse(JSON.stringify(this.queryParams))
      delete params.data
      projectApplyList(params).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false
      })
    },
    handleClose() {
      this.dialogVisible = false;
    },
    handleOption(row) {
      checkContractCreate({
        id: row.id
      }).then(res => {
        if (!res.data && !this.checkShow) {
          this.$message.error('当前项目已在合同制定的签署流程过程中');
          return;
        }

        projectApplyInfo({
          id: row.id
        }).then(infoRes => {
          this.$emit('ok', {
            ...row,
            ...infoRes.data
          });
          this.handleClose();
        })
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.beginTime = this.queryParams.data[0]
      this.queryParams.endTime = this.queryParams.data[1]
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        projectCode: '',
        customerType: '',
        customerNickName: '',
        submitAgentNickName: '',
        beginTime: '',
        endTime: '',
        data: []
      };
      this.getList();
    }
  }
};
</script>
<style lang="scss" scoped>
</style>